%%% Copyright 2010 Google Inc.
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%%     http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.

%%% @copyright 2010 Google Inc. All Rights Reserved.
%%% @author <jacoby@google.com> Jacoby Thwaites [http://sites/madcloud]
%%% @version {@version}
%%% @doc
%%% Mock client handling view_fsm callbacks for testing.
%%%
%%% This client acts as callback for two view_fsm instances, one on the
%%% /mock folder, and one on the /mock/backend folder.

-module(client_mock).

-include("client.hrl").

-export([attach/1, start/1, handle_input/4]).

%% Attach specification to root folder of primes.
attach_spec() ->
  #?TXM_ATTACH_SPEC{
                prog = "/mock/Primes",
                version = none,
                cookie = "my_cookie"}.

%% Attaches a view to the root folder.
%% Notify event creates a new process on the machine.
attach(frontend) ->

  % Create and attach a view to the toplevel folder.
  {ok, View1} = view_fsm:start(?MODULE),
  AttachSpec = attach_spec(),
  ok = view_fsm:attach(View1, AttachSpec#?TXM_ATTACH_SPEC{folder = "frontend"}),
  View1;

%% Attaches a view to the backend folder.
attach(backend) ->

  % Create and attach a view to the backend folder.
  {ok, View2} = view_fsm:start(?MODULE),
  AttachSpec = attach_spec(),
  ok = view_fsm:attach(View2, AttachSpec#?TXM_ATTACH_SPEC{folder = "backend"}),
  View2.

%% Fires the notification against the Start op on the frontend view.
start(Frontend) ->

  % Fire the start event.
  view_fsm:notify(Frontend, "Start", "Start", [{"n", 1000000013}]).

%% ----------------------------------------------------------------------------
%% Input handlers for toplevel folder.
%% ----------------------------------------------------------------------------
handle_input(_View, _Ref, "ShowNotPrime", Fields) ->
  N = proplists:get_value("n", Fields),
  io:format("~p is not prime~n", [N]);

handle_input(_View, _Ref, "ShowPrime", Fields) ->
  N = proplists:get_value("n", Fields),
  io:format("~p is prime~n", [N]);

%% ----------------------------------------------------------------------------
%% Handlers for backend folder.
%% ----------------------------------------------------------------------------
handle_input(View, Ref, "FirstDivisor", [{"n", N}]) ->
  io:format("First divisor for ~p~n", [N]),
  view_fsm:reply(View, Ref, "Divisor", [{"div", 3}]);

handle_input(View, Ref, "Test", Fields) ->
  N = proplists:get_value("n", Fields),
  D = proplists:get_value("div", Fields),
  io:format("Testing ~p and ~p~n", [N, D]),
  case {D > math:sqrt(N), N rem D} of
    {true, _} -> view_fsm:reply(View, Ref, "Yes", [{"YES", true}]);
    {false, 0} -> view_fsm:reply(View, Ref, "No", [{"NO", true}]);
    {false, _} -> view_fsm:reply(View, Ref, "Maybe", [{"MAYBE", true}])
  end;

handle_input(View, Ref, "Iterate", Fields) ->
  N = proplists:get_value("n", Fields),
  D = proplists:get_value("div", Fields),
  io:format("Next divisor ~p~n", [D]),
  view_fsm:reply(View, Ref, "Iterate",
                 [{"n", N}, {"div", D + 2}]).
